package com.dituhui.tool.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.ESTool;
import com.dituhui.tool.io.FileTool;
import com.dituhui.tool.io.HttpTool;

/**
 * ES导数据工具
 * 
 * @author liweigu
 *
 */
public class ESDataUtil {

	public static void main(String[] args) {
		String output = Constants.getValue("exportES.output", "config.properties");
		String host = Constants.getValue("exportES.HOST", "config.properties");
		int httpPort = Integer.parseInt(Constants.getValue("exportES.PORT_HTTP", "config.properties"));
		int tcpPort = Integer.parseInt(Constants.getValue("exportES.PORT_TCP", "config.properties"));
		String prefix = Constants.getValue("exportES.prefix", "config.properties");
		System.out.println("prefix=" + prefix);
		// System.out.println("StringUtil.COMMA=" + StringUtil.COMMA);
		// if (prefix.contains(StringUtil.VerticalLine)) {
		// // 前缀词有多个
		// String[] prefixes = prefix.split(StringUtil.VerticalLine);
		// for (String subPrefix : prefixes) {
		// System.out.println("subPrefix=" + subPrefix);
		// }
		// }

		outputData(host, httpPort, tcpPort, prefix, output);
	}

	public static void uploadData(String host, int httpPort, int tcpPort, String prefix, String output) {

	}

	/**
	 * 导出ES数据
	 * 
	 * @param host
	 * @param httpPort
	 * @param tcpPort
	 * @param prefix
	 * @param output
	 */
	public static void outputData(String host, int httpPort, int tcpPort, String prefix, String output) {
		long startTime = System.currentTimeMillis();
		List<String> indexList = getIndexList(host, httpPort, prefix);
		System.out.println("index count: " + indexList.size());
		String esUrl = host + StringUtil.COLON + tcpPort;
		for (String index : indexList) {
			// System.out.println(index);
			String[] queryColumnNames = null;
			String[] queryColumnValues = null;
			List<String> dataList = ESTool.query(index, queryColumnNames, queryColumnValues, esUrl);
			String fileName = output + index + ".data";
			FileTool.writeFile(fileName, (ArrayList<String>) dataList);
		}
		long endTime = System.currentTimeMillis();
		long t = (endTime - startTime) / 1000;
		System.out.println("导出完成。耗时(s)：" + t);
	}

	/**
	 * 获取索引列表
	 * 
	 * @param host ES服务地址
	 * @param port ES服务端口
	 * @param prefix 前缀搜索关键词。如果有多个关键词，以|分隔。
	 * @return 索引列表
	 */
	private static List<String> getIndexList(String host, int port, String prefix) {
		List<String> indexList = new ArrayList<String>();

		if (!host.startsWith("http")) {
			host = "http://" + host;
		}

		String path = host + StringUtil.COLON + port + "/_cat/indices";
		String param = null;
		try {
			// eg:
			// yellow open .monitoring-es-2-2017.05.16 J5CUyIZGQfmleFPy_vDFQQ 1 1 19837 192 9.3mb 9.3mbyellow open
			// district_00000000000000000000000000000000_10000_0 3XYVlXnhT2SYwQNYxEug1A 5 1 44084 0 4.4gb 4.4gbyellow open .monitoring-es-2-2017.05.13
			// e3_QpMM0SUWa52Nee9jIng 1 1 16123 210 7.2mb 7.2mbyellow open .monitoring-es-2-2017.05.11 nVcMjRLuSraVMYtzsrzfFQ 1 1 13647 266 6.1mb 6.1mbyellow
			// open .monitoring-es-2-2017.05.17 wLhm6Wh6R9ydeXhdPfbMlA 1 1 5987 230 3mb 3mbred open .monitoring-data-2 z0OjrwTmSM23pmX2P9wceQ 1 1 yellow open
			// .monitoring-es-2-2017.05.12 HSY4GHHsS0y3laG92qIr3w 1 1 14885 240 6.7mb 6.7mbyellow open district_00000000000000000000000000000000_10000_200
			// ODZ2mPwMQPGUBzhStqbFIA 5 1 44084 0 925mb 925mbyellow open .monitoring-es-2-2017.05.15 EuuLggTcQf-QfvY8PkT_vQ 1 1 18599 92 8.3mb 8.3mbyellow open
			// district_dict 4sAZpISRSJyO4ET3BC9hVA 5 1 2 0 9.2kb 9.2kbyellow open .monitoring-es-2-2017.05.14 Z4khdi9MQz2U8umE2lhAvQ 1 1 17375 308 7.8mb 7.8mb
			String content = HttpTool.getContentByGET(path, param);
			// System.out.println(content);
			String[] arr = content.split(StringUtil.SPACING);
			boolean isBeforeIndexName = false;
			for (String s : arr) {
				if (isBeforeIndexName) {
					if (s.length() > 0) {
						if (prefix == null || prefix.length() == 0) {
							// 不需要前缀筛选
							indexList.add(s);
						} else {
							// 需要前缀筛选
							if (prefix.contains(StringUtil.COMMA)) {
								// 前缀词有多个
								String[] prefixes = prefix.split(StringUtil.COMMA);
								for (String subPrefix : prefixes) {
									if (subPrefix.length() > 0 && s.startsWith(subPrefix)) {
										// System.out.println("subPrefix=" + subPrefix + ",s=" + s);
										indexList.add(s);
										break;
									}
								}
							} else {
								// 前缀词只有一个
								if (s.startsWith(prefix)) {
									indexList.add(s);
								}
							}
						}
						isBeforeIndexName = false;
					}
				} else {
					// open 之后是 index 名称
					if ("open".equalsIgnoreCase(s)) {
						isBeforeIndexName = true;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return indexList;
	}
}
